import { useEffect } from "./useEffect";

/**
 * requestAnimationFrame封装的钩子
 * @param fn
 */
export function useRaf(fn: () => void) {
  useEffect(() => {
    let animationId: number;
    const callback: FrameRequestCallback = () => {
      fn();
      animationId = requestAnimationFrame(callback);
    };

    animationId = requestAnimationFrame(callback);
    return () => {
      cancelAnimationFrame(animationId);
    };
  });
}
